package com.fmatos.amazingwallpapers.image.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;

import org.joda.time.DateTime;

import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;

import com.fmatos.amazingwallpapers.image.network.dto.RemoteAlbumInfo;
import com.fmatos.amazingwallpapers.imagemanagement.FileManager;
import com.google.gson.Gson;

/**
 * Connects to cloud services and bring data
 * @author fdematos
 *
 */
public class CloudClient extends ContextWrapper {

//	private static final String HEROKU_BASE_URL = "http://amazing-wallpapers.heroku.com";
	public static final String HEROKU_BASE_URL = "http://amazing-wallpapers.herokuapp.com/";
//	private static final String HEROKU_BASE_URL = "amazing-wallpapers.heroku.com";
	private static final String HEROKU_URL_ALBUMS_LIST = "/album";
	public static final String HEROKU_URL_PING = "";
	
	private static final String TAG = CloudClient.class.getSimpleName();

	protected FileManager fileManager;
	protected CloudHelper cloudHelper;
	
	public CloudClient(Context context) {
		super(context);
		acquireResources();
	}
	
	protected void acquireResources() {
		fileManager = new FileManager(getApplicationContext());
		cloudHelper = new CloudHelper();
	}

	protected String getAlbumsListUrl() {
		return HEROKU_URL_ALBUMS_LIST;
	}
	
	/**
	 * Gets a listing of all albums available 
	 * @return
	 */
	public List<RemoteAlbumInfo> getAlbumsList() {
		
		if ( ! CloudPing.isServerAlive() ) {
			return null;
		}
		
		URLConnection connection = cloudHelper.connectToUrl(HEROKU_BASE_URL + getAlbumsListUrl() );
		List<RemoteAlbumInfo> albumsList = getAlbumFromConnection(connection);
		
		return albumsList;
	}

	/**
	 * Returns album meta information and photo urls from connection
	 * @param url
	 * @return
	 */
	public RemoteAlbumInfo getAlbumInfo(String url) {

		if ( ! CloudPing.isServerAlive() ) {
			return null;
		}
		
		URLConnection connection = cloudHelper.connectToUrl(HEROKU_BASE_URL + url );
		
		RemoteAlbumInfo album =  getAlbumInfoFromConnection(connection);
		return album;

	}
	

	private List<RemoteAlbumInfo> getAlbumFromConnection(URLConnection connection) {
	
		StringBuilder builder = cloudHelper.getStringFromConnection(connection);
		if (builder == null ) {
			return null;
		}

		Gson gson = new Gson();
		
		RemoteAlbumInfo[] albumList = gson.fromJson(builder.toString(), RemoteAlbumInfo[].class);
		
		if ( albumList != null ) {
			return Arrays.asList(albumList);
		} else {
			return null;
		}
	}
	
	private RemoteAlbumInfo getAlbumInfoFromConnection(URLConnection connection) {
		
		StringBuilder builder = cloudHelper.getStringFromConnection(connection);

		if ( builder != null ) {
			Gson gson = new Gson();
			RemoteAlbumInfo albumInfo = gson.fromJson(builder.toString(), RemoteAlbumInfo.class);
			return albumInfo;
		}
		
		return null;
	}


	public String getPhotoFromNetwork(String thumbnailUrl) {

		InputStream istream = cloudHelper.downloadImage(thumbnailUrl);
		
		String path = null;
		if ( istream != null ) {
			path = fileManager.writeFileToRandomPath(istream, "cloudManager-");
			if (! fileManager.isImageHealthy(path)) {
				path = null;
			}
		}
		
		return path;
	}

}
